package com.windupurnomo;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.windupurnomo.data.ActivityManagerData;
import com.windupurnomo.entity.ListPlayer;
import com.windupurnomo.entity.Player;
import com.windupurnomo.entity.ShareInfo;
import com.windupurnomo.util.MyQuery;

public class BestPlayer extends ListActivity{
	private ShareInfo shareInfo = new ShareInfo();
	private ProgressDialog m_ProgressDialog = null;
    private ArrayList<Player> m_tasks = null;
    private TaskAdapter m_adapter;
    private Runnable viewOrders;
    private ListPlayer lt = null;
    private Player player;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.best_player);
        
        m_tasks = new ArrayList<Player>();
        ActivityManagerData acm = new ActivityManagerData(this);
        SQLiteDatabase db = acm.getReadableDatabase();
        List<Player> lp = MyQuery.PlayerDAO.best5(db);
        lt = new ListPlayer();
        lt.setPlayers(lp);
        this.m_adapter = new TaskAdapter(this, R.layout.row, m_tasks);
        setListAdapter(this.m_adapter);
       
        viewOrders = new Runnable(){
            public void run() {
                getOrders();
            }
        };
        Thread thread =  new Thread(null, viewOrders, "MagentoBackground");
        thread.start();
        m_ProgressDialog = ProgressDialog.show(BestPlayer.this,"Please wait...", "Retrieving data ...", true);
    }
    
    private Runnable returnRes = new Runnable() {

        public void run() {
            if(m_tasks != null && m_tasks.size() > 0){
                m_adapter.notifyDataSetChanged();
                for(int i=0;i<m_tasks.size();i++)
                m_adapter.add(m_tasks.get(i));
            }
            m_ProgressDialog.dismiss();
            m_adapter.notifyDataSetChanged();
        }
    };
    
    private void getOrders(){
          try{
              m_tasks = new ArrayList<Player>();
              m_tasks = (ArrayList<Player>) lt.getPlayers();
              Thread.sleep(0);
              Log.i("ARRAY", ""+ m_tasks.size());
            } catch (Exception e) {
              Log.e("BACKGROUND_PROC", e.getMessage());
            }
          runOnUiThread(returnRes);
    }
    
    private class TaskAdapter extends ArrayAdapter<Player> {
        private ArrayList<Player> items;
        public TaskAdapter(Context context, int textViewResourceId, ArrayList<Player> items) {
                super(context, textViewResourceId, items);
                this.items = items;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.row, null);
                }
                Player t = items.get(position);
                if (t != null) {
                        TextView tt = (TextView) v.findViewById(R.id.toptext);
                        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                        if (tt != null) {
                              tt.setText("Name: "+t.getName());
                         }
                        if(bt != null){
                        	  String score = String.valueOf(t.getScore());
                              bt.setText("Score: "+score);
                        }
                }
                return v;
        }
    }
    
    protected void onListItemClick(ListView list, View v, int position, long id){
		Intent i = new Intent(this, SelectedPlayer.class);
		long ltId = list.getItemIdAtPosition(position);
		player = lt.getPlayers().get((int)ltId);
		Log.i("acmWindu", player.getName());
		shareInfo.setPlayer(player);
		i.putExtra("shareInfo", shareInfo);
		startActivity(i);
	}
}
